<html>
<head><meta charset="utf-8"><title>call for participation: create method from usage · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html">call for participation: create method from usage</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="248209003"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248209003" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248209003">(Aug 03 2021 at 11:30)</a>:</h4>
<p>Hey, I've been wanting this badly for some months now: <a href="https://github.com/rust-analyzer/rust-analyzer/issues/8555">https://github.com/rust-analyzer/rust-analyzer/issues/8555</a></p>
<p>I think it mostly has all the required infra (we already do that for free-standing functions), so bring that over the finish line shouldn't be too hard. <span class="user-mention" data-user-id="424024">@Mahdi Faramarzpour</span> I wonder if you might be interested in that</p>



<a name="248229565"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248229565" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mahdi Faramarzpour <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248229565">(Aug 03 2021 at 14:36)</a>:</h4>
<blockquote>
<p><span class="user-mention silent" data-user-id="424024">Mahdi Faramarzpour</span> I wonder if you might be interested in that</p>
</blockquote>
<p>Yeah why not! of course with guidance.</p>



<a name="248229688"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248229688" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248229688">(Aug 03 2021 at 14:37)</a>:</h4>
<p><a href="https://github.com/rust-analyzer/rust-analyzer/blob/314e2e75c09cc6115fc35fd94433b146eb3e325a/crates/ide_assists/src/handlers/generate_function.rs#L45">https://github.com/rust-analyzer/rust-analyzer/blob/314e2e75c09cc6115fc35fd94433b146eb3e325a/crates/ide_assists/src/handlers/generate_function.rs#L45</a></p>



<a name="248229736"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248229736" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248229736">(Aug 03 2021 at 14:38)</a>:</h4>
<p>this is the main thing to look at</p>



<a name="248229861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248229861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248229861">(Aug 03 2021 at 14:39)</a>:</h4>
<p>Note that the existing impl is mostly us figuring stuff out -- if something doesn't make sense, feel free to refactor!</p>



<a name="248234974"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248234974" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248234974">(Aug 03 2021 at 15:14)</a>:</h4>
<p>As a proof of "we're figuring this out", generate_function now has the following crash: </p>
<div class="codehilite"><pre><span></span><code>Panic context:
&gt;
version: 314e2e75c 2021-08-02 dev
request: textDocument/codeAction CodeActionParams {
    text_document: TextDocumentIdentifier {
        uri: Url {
            scheme: &quot;file&quot;,
            cannot_be_a_base: false,
            username: &quot;&quot;,
            password: None,
            host: None,
            port: None,
            path: &quot;/home/matklad/projects/rust-analyzer/crates/hir_def/src/resolver.rs&quot;,
            query: None,
            fragment: None,
        },
    },
    range: Range {
        start: Position {
            line: 795,
            character: 12,
        },
        end: Position {
            line: 795,
            character: 15,
        },
    },
    context: CodeActionContext {
        diagnostics: [],
        only: None,
    },
    work_done_progress_params: WorkDoneProgressParams {
        work_done_token: None,
    },
    partial_result_params: PartialResultParams {
        partial_result_token: None,
    },
}

thread &#39;&lt;unnamed&gt;&#39; panicked at &#39;Failed to make ast node `syntax::ast::generated::nodes::Name` from text mod 0;&#39;, crates/syntax/src/ast/make.rs:641:13
stack backtrace:
   0: rust_begin_unwind
             at /rustc/3c216f01c7be6a01cfca069eee62ef3c72d9fd3a/library/std/src/panicking.rs:515:5
   1: std::panicking::begin_panic_fmt
             at /rustc/3c216f01c7be6a01cfca069eee62ef3c72d9fd3a/library/std/src/panicking.rs:457:5
   2: syntax::ast::make::ast_from_text
   3: syntax::ast::make::name
   4: core::ops::function::impls::&lt;impl core::ops::function::FnOnce&lt;A&gt; for &amp;mut F&gt;::call_once
   5: itertools::Itertools::join
   6: syntax::ast::make::param_list
   7: ide_assists::handlers::generate_function::generate_function
   8: ide_assists::assists
   9: ide::Analysis::assists_with_fixes::{{closure}}
  10: std::panicking::try
  11: ide::Analysis::assists_with_fixes
  12: rust_analyzer::handlers::handle_code_action
  13: &lt;F as threadpool::FnBox&gt;::call_box
</code></pre></div>



<a name="248237647"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248237647" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mahdi Faramarzpour <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248237647">(Aug 03 2021 at 15:32)</a>:</h4>
<p>Yeah, that's basically it when we go for text-based solutions. For a moment I thought it's my code that's causing it.</p>



<a name="248497880"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248497880" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mahdi Faramarzpour <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248497880">(Aug 05 2021 at 16:00)</a>:</h4>
<p><span class="user-mention" data-user-id="133169">@matklad</span>  What sort of behavior is expected?<br>
let's say there is no impl block for that type. In that case it's clear that we should create a block; but where? In the  module that the type itself resides, or the module we're currently typing in? Also, if a block already exists but it's not in the module we're typing in, should we create another block in that module?</p>



<a name="248498098"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248498098" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248498098">(Aug 05 2021 at 16:01)</a>:</h4>
<p>Usually, there's no one single clear answer to these sorts of questions. The best strategy is to go with the simplest and most "obvious" approach.</p>



<a name="248498278"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248498278" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248498278">(Aug 05 2021 at 16:02)</a>:</h4>
<p>In this case, I think if there's an existing impl block, we should apped function there. If there's no block, we should create one in the same file that the type is denined in</p>



<a name="248498316"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248498316" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248498316">(Aug 05 2021 at 16:02)</a>:</h4>
<p>I <em>think</em> this logic is already wirtten for the "generate_getter" assist</p>



<a name="248498370"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248498370" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248498370">(Aug 05 2021 at 16:03)</a>:</h4>
<p>Next to the definition seems reasonable, at least if it's the user's type.</p>



<a name="248498650"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248498650" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mahdi Faramarzpour <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248498650">(Aug 05 2021 at 16:05)</a>:</h4>
<p>right, and how can I turn an <code>hir::Type</code> to a <code>ast::Type</code> with the use of a <code>semantics&lt;RootDatabase&gt;</code>?</p>



<a name="248498807"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248498807" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mahdi Faramarzpour <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248498807">(Aug 05 2021 at 16:07)</a>:</h4>
<p>I'm quite stuck on this <span aria-label="thinking" class="emoji emoji-1f914" role="img" title="thinking">:thinking:</span></p>



<a name="248498975"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248498975" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248498975">(Aug 05 2021 at 16:08)</a>:</h4>
<p>See the <code>HirDisplay</code> trait, it might help</p>



<a name="248499019"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248499019" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248499019">(Aug 05 2021 at 16:08)</a>:</h4>
<p>I don't think we have anthing to convert to ast yet (but that would be useful)</p>



<a name="248499023"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248499023" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248499023">(Aug 05 2021 at 16:08)</a>:</h4>
<p>Simplest is by turning it into an adt <code>ty.as_adt()</code> and then calling <code>source</code> the variants of that I believe</p>



<a name="248499130"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248499130" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> matklad <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248499130">(Aug 05 2021 at 16:10)</a>:</h4>
<p>See also <code>make_ty</code> in <code>extract_function.rs</code></p>



<a name="248500368"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248500368" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mahdi Faramarzpour <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248500368">(Aug 05 2021 at 16:20)</a>:</h4>
<blockquote>
<p>I don't think we have anthing to convert to ast yet</p>
</blockquote>
<p>Well, I need to have the ast node of that adt to find the impl blocks. There's a function which does that, and uses ast nodes rather than semantic data.</p>



<a name="248500504"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248500504" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248500504">(Aug 05 2021 at 16:21)</a>:</h4>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">match</span><span class="w"> </span><span class="n">ty</span><span class="p">.</span><span class="n">as_adt</span><span class="p">()</span><span class="o">?</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">hir</span>::<span class="n">Adt</span>::<span class="n">Struct</span><span class="p">(</span><span class="n">strukt</span><span class="p">)</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="n">strukt</span><span class="p">.</span><span class="n">source</span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="o">..</span><span class="p">.</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>Should give you the ast nodes</p>



<a name="248500603"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248500603" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248500603">(Aug 05 2021 at 16:22)</a>:</h4>
<p>For primitives(<code>u8</code>, <code>str</code>, etc) this doesn't work of course but those don't have source definitions anyways</p>



<a name="248500658"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248500658" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lukas Wirth <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248500658">(Aug 05 2021 at 16:23)</a>:</h4>
<p>you also may have to strip references before doing this as well so you might need to do <code>ty.strip_references().as_adt()</code></p>



<a name="248501267"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248501267" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Laurențiu <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248501267">(Aug 05 2021 at 16:28)</a>:</h4>
<p>"Create extension method" would be <span aria-label="love" class="emoji emoji-2764" role="img" title="love">:love:</span></p>



<a name="248710556"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/call%20for%20participation%3A%20create%20method%20from%20usage/near/248710556" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Mahdi Faramarzpour <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/call.20for.20participation.3A.20create.20method.20from.20usage.html#248710556">(Aug 07 2021 at 10:57)</a>:</h4>
<p>Done:<br>
<a href="https://github.com/rust-analyzer/rust-analyzer/pull/9804">https://github.com/rust-analyzer/rust-analyzer/pull/9804</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>